<!DOCTYPE html>
<title>DOMStringList interface</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>

// Returns a promise that will resolve to a DOMStringList.
// Strings must be unique, and the strings will be sorted.
function makeDOMStringList(entries) {
    return new Promise(function(resolve, reject) {
        var dbname = String(self.location);
        var r = indexedDB.open(dbname);
        r.onerror = function() { reject(r.error); };
        r.onupgradeneeded = function() {
            var db = r.result;
            entries.forEach(function(entry) {
                db.createObjectStore(entry);
            });
        }
        r.onsuccess = function() {
            var db = r.result;
            var dsl = db.objectStoreNames;
            db.close();
            var r2 = indexedDB.deleteDatabase(dbname);
            r2.onerror = function() { reject(r2.error); };
            r2.onsuccess = function() { resolve(dsl); };
        }
    });
}

promise_test(function(t) {
    return makeDOMStringList(['a', 'b', 'c'])
        .then(function(dsl) {
            assert_equals(Object.prototype.toString.call(dsl),
                          '[object DOMStringList]',
                          'Object should be branded as "DOMStringList"');
            assert_true('length' in dsl && typeof dsl.length === 'number',
                        'DOMStringList should have length attribute');
            assert_true('item' in dsl && typeof dsl.item === 'function',
                        'DOMStringList should have item method');
            assert_true('contains' in dsl && typeof dsl.contains === 'function',
                        'DOMStringList should have contains method');

            assert_equals(dsl.length, 3, 'length attribute');

            assert_equals(dsl.item(0), 'a', 'item method');
            assert_equals(dsl.item(1), 'b', 'item method');
            assert_equals(dsl.item(2), 'c', 'item method');
            assert_equals(dsl.item(3), null, 'item method out of range');
            assert_throws_js(
                TypeError, function() { dsl.item(); },
                'item method should throw if called without enough args');

            assert_equals(dsl[0], 'a', 'indexed getter');
            assert_equals(dsl[1], 'b', 'indexed getter');
            assert_equals(dsl[2], 'c', 'indexed getter');
            assert_equals(dsl[3], undefined, 'indexed getter out of range');

            assert_true(dsl.contains('a'),
                        'contains method matched');
            assert_true(dsl.contains('b'),
                        'contains method matched');
            assert_true(dsl.contains('c'),
                        'contains method matched');
            assert_false(dsl.contains(''),
                         'contains method unmatched');
            assert_false(dsl.contains('d'),
                         'contains method unmatched');
            assert_throws_js(
                TypeError, function(){ dsl.contains(); },
                'contains method should throw if called without enough args');
        });
}, 'DOMStringList interface');

</script>
